package com.syaccp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.syaccp.mapper.CustomerCouponMapper;
import com.syaccp.model.CustomerCoupon;
import com.syaccp.model.CustomerUseCoupon;
import com.syaccp.service.CustomerCouponService;
import com.syaccp.service.CustomerUseCouponService;
import com.syaccp.utils.DateUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 针对表【t_customer_coupon(客户优惠卷表)】的数据库操作Service实现
 *
 * @author Lyh
 * @since 2023-07-19 15:27:05
 */
@Service
public class CustomerCouponServiceImpl extends ServiceImpl<CustomerCouponMapper, CustomerCoupon>
        implements CustomerCouponService {

    @Resource
    CustomerCouponMapper customerCouponMapper;

    @Resource
    CustomerUseCouponService customerUseCouponService;

    @Override
    public List<CustomerCoupon> queryCustomerCoupon(Integer userId) {
        LambdaQueryWrapper<CustomerCoupon> lqw = Wrappers.lambdaQuery();
        lqw.eq(CustomerCoupon::getCustomerId, userId)
                .eq(CustomerCoupon::getStatus, 1)
                .lt(CustomerCoupon::getStartTime, DateUtils.getTodayDateTime())
                .gt(CustomerCoupon::getEndTime, DateUtils.getTodayDateTime())
                .orderByAsc(CustomerCoupon::getMoney);
        return customerCouponMapper.selectList(lqw);
    }

    @Override
    public Integer useCustomerCoupon(Integer couponId, Integer orderId) {
        LambdaUpdateWrapper<CustomerCoupon> luw = Wrappers.lambdaUpdate();
        luw.eq(CustomerCoupon::getId, couponId);
        CustomerUseCoupon cuc = new CustomerUseCoupon();
        cuc.setCouponId(couponId);
        cuc.setOrderId(orderId);
        cuc.setCreateDate(DateUtils.getTodayLocalDateTime());
        customerUseCouponService.insertCustomerUseCouponRecord(cuc);
        return customerCouponMapper.update(
                new CustomerCoupon()
                        .setStatus(2)
                        .setUpdateTime(DateUtils.getTodayLocalDateTime()), luw);
    }
}




